package com.example.backend.repository;

import com.example.backend.domain.entity.Notification;
import com.example.backend.domain.enums.NotificationChannel;
import com.example.backend.domain.enums.NotificationPriority;
import com.example.backend.domain.enums.NotificationStatus;
import com.example.backend.domain.enums.NotificationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;

/**
 * 通知数据访问层
 */
@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long>, JpaSpecificationExecutor<Notification> {
    
    // 基础查询
    Page<Notification> findByUserId(Long userId, Pageable pageable);
    Page<Notification> findByUserIdAndStatus(Long userId, NotificationStatus status, Pageable pageable);
    Page<Notification> findByUserIdAndType(Long userId, NotificationType type, Pageable pageable);
    Page<Notification> findByUserIdAndChannel(Long userId, NotificationChannel channel, Pageable pageable);
    
    // 统计查询
    long countByUserIdAndStatus(Long userId, NotificationStatus status);
    long countByUserIdAndPriority(Long userId, NotificationPriority priority);
    long countByUserIdAndType(Long userId, NotificationType type);
    
    // 时间范围查询
    @Query("SELECT COUNT(n) FROM Notification n WHERE n.userId = :userId AND n.createdAt >= :from AND n.createdAt <= :to")
    long countByUserIdAndCreatedAtBetween(@Param("userId") Long userId, @Param("from") Instant from, @Param("to") Instant to);
    
    // 批量更新
    @Modifying
    @Query("UPDATE Notification n SET n.status = :status, n.readAt = :readAt WHERE n.id IN :ids")
    int updateStatusByIds(@Param("ids") List<Long> ids, @Param("status") NotificationStatus status, @Param("readAt") Instant readAt);
    
    @Modifying
    @Query("UPDATE Notification n SET n.status = :status WHERE n.userId = :userId AND n.status = :oldStatus")
    int updateStatusByUserId(@Param("userId") Long userId, @Param("oldStatus") NotificationStatus oldStatus, @Param("status") NotificationStatus status);
    
    // 删除查询
    @Modifying
    @Query("DELETE FROM Notification n WHERE n.id IN :ids")
    int deleteByIds(@Param("ids") List<Long> ids);
    
    @Modifying
    @Query("DELETE FROM Notification n WHERE n.userId = :userId AND n.status = :status")
    int deleteByUserIdAndStatus(@Param("userId") Long userId, @Param("status") NotificationStatus status);
    
    // 搜索查询
    @Query("SELECT n FROM Notification n WHERE n.userId = :userId AND " +
           "(LOWER(n.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(n.content) LIKE LOWER(CONCAT('%', :keyword, '%')))")
    Page<Notification> searchByUserIdAndKeyword(@Param("userId") Long userId, @Param("keyword") String keyword, Pageable pageable);
    
    // 获取未读通知
    @Query("SELECT n FROM Notification n WHERE n.userId = :userId AND n.status = 'UNREAD' ORDER BY n.priority DESC, n.createdAt DESC")
    List<Notification> findUnreadByUserId(@Param("userId") Long userId);
    
    // 获取紧急通知
    @Query("SELECT n FROM Notification n WHERE n.userId = :userId AND n.priority IN ('HIGH', 'URGENT') AND n.status = 'UNREAD'")
    List<Notification> findUrgentByUserId(@Param("userId") Long userId);
}
